package com.example.hidesms.bo;

import java.util.ArrayList;

import android.database.Cursor;

import com.example.hidesms.dao.BlockCallDAO;
import com.example.hidesms.dao.SmsDAO;
import com.example.hidesms.dto.BlockCallItem;
import com.example.hidesms.sqlite.DataBaseHelper;

public class BlockCallBO implements IBlockCallBO{
	private BlockCallItem				sms;
	private ArrayList<BlockCallItem>	listSms;
	private static BlockCallBO		instance;
	

	public static BlockCallBO getInstance() {
		if (instance == null) {
			instance = new BlockCallBO();
		}

		return instance;
	}
	private BlockCallItem fetchFromCurcor(Cursor c) {
		sms = new BlockCallItem();
		sms.setId(c.getInt(c.getColumnIndex("CallID")));
		sms.setName(c.getString(c.getColumnIndex("Name")));
		sms.setNumber(c.getString(c.getColumnIndex("Number")));
		sms.setImage(c.getBlob(c.getColumnIndex("Image")));
		sms.setIsComing(c.getString(c.getColumnIndex("IsComing")));

		return sms;
	}

	@Override
	public boolean insert(DataBaseHelper myDatabase, BlockCallItem sms) {
		// TODO Auto-generated method stub
		return BlockCallDAO.getInstance().insert(myDatabase, sms);
	}

	@Override
	public boolean update(DataBaseHelper myDatabase, BlockCallItem sms) {
		// TODO Auto-generated method stub
		return BlockCallDAO.getInstance().update(myDatabase, sms);
	}

	@Override
	public boolean delete(DataBaseHelper myDatabase, int smsId) {
		// TODO Auto-generated method stub
		return SmsDAO.getInstance().delete(myDatabase, smsId);
	}

	@Override
	public ArrayList<BlockCallItem> getAll(DataBaseHelper myDatabase, String folderGroup, boolean flag) {
		// TODO Auto-generated method stub
		listSms = new ArrayList<BlockCallItem>();
		Cursor c= BlockCallDAO.getInstance().getAll(myDatabase, folderGroup, flag);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}

	@Override
	public ArrayList<BlockCallItem> getById(DataBaseHelper myDatabase, int smsId) {
		listSms = new ArrayList<BlockCallItem>();
		Cursor c= BlockCallDAO.getInstance().getById(myDatabase, smsId);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}
	@Override
	public ArrayList<BlockCallItem> getAllByRead(DataBaseHelper myDatabase, String read) {
		listSms = new ArrayList<BlockCallItem>();
		Cursor c= BlockCallDAO.getInstance().getAllByRead(myDatabase, read);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}
	@Override
	public ArrayList<BlockCallItem> getByAdress(DataBaseHelper myDatabase, String address) {
		listSms = new ArrayList<BlockCallItem>();
		Cursor c= BlockCallDAO.getInstance().getByAdress(myDatabase, address);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}

}

